<?php
namespace App\Services\Trends;

use App\Model\Image;
use App\Model\Tag;
use App\Model\Trends;
use App\Services\CommonService;
use Illuminate\Support\Facades\DB;

class TrendsService extends CommonService
{
    /**
     * @return \Illuminate\Http\JsonResponse
     * 查询动态
     */
    public function sel()
    {
        //查询动态表和用户表联查获取20条数据
        $data=Trends::orderBy("trends_time","desc")
            ->join('re_user', 're_user.id', '=', 're_trends.user_id')
            ->get(['trends_id','user_id','trends_time','trends_img','trends_type','trends_content','u_label_id','u_name'])
            ->take(20)
            ->toArray();
        //判断数据是否有效
        if($data===array())
        {
            return $this->fail(204,"查询动态数据失败");
        }
        //定义Img为空字符串
        $Img="";
        //定义tag为空字符串
        $Tag="";
        //循环拼接照片表的id和标签表的id
        foreach($data as $k=>$v)
        {
            $Img.=$v['trends_img'].",";
            $Tag.=$v['u_label_id'].",";
        }
        //截取最后一个，
        $Img=mb_substr($Img,0,-1);
        $Tag=mb_substr($Tag,0,-1);
        //分割成数组
        $ImgArray=explode(",",$Img);
        $TagArray=explode(",",$Tag);
        //去掉相同的数据
        $ImgArray=array_unique($ImgArray);
        $TagArray=array_unique($TagArray);
        //查照片表根据照片id
        $ImgInfo=Image::all()
            ->whereIn("id",$ImgArray)
            ->toArray();
        //查标签表根据标签id
        $TagInfo=Tag::all()
            ->whereIn("reg_id",$TagArray)
            ->toArray();
        //循环拼接数据

        foreach($data as &$v)
        {
            $v['trends_time']=date("Y-m-d H:i:s",$v['trends_time']);
            $ImgArray=explode(",",$v['trends_img']);
            foreach($ImgInfo as $kk=>$vv)
            {
                if(in_array($vv['id'],$ImgArray)){
                    $v['img'][]=$vv['img_url'];
                }
            }

            $TagArray=explode(",",$v['u_label_id']);
            foreach($TagInfo as $kk=>$vv)
            {
                if(in_array($vv['reg_id'],$TagArray)){
                    $v['tag'][]=$vv['reg_name'];
                }
            }
            $v['u_label_id']=implode(" | ",$v['tag']);
        }

        return $this->success($data);
    }

    /**
     * @param $request
     * @return \Illuminate\Http\JsonResponse
     * 添加动态
     */
    public function add($request){
        //接值post
        $text=$request->post('text');
        //判断是否有照片
        $img=$request->post('img');

        if (!empty($img)) {
            $img=substr($img,0,-1);
            $img=explode(",",$img);
            //循环拼接数据去添加
            foreach($img as $v)
            {
                $ImgArray[]['img_url']=$v;
            }
            //一下添加好几条数据
            $ImgInfo=DB::table("re_image")->insert($ImgArray);
            //判断添加是否成功
            if($ImgInfo!==true)
            {
               return $this->fail(206,'添加视频或照片失败');
            }
            //获取刚添加的id
            $ImgId=Image::get(['img_url','id'])
                ->whereIn("img_url",$img)
                ->toArray();
            //使用数组函数把id挑出来
            $id_list = array_column($ImgId, 'id');
            //把id换成1,2,3类型
            $img_id=implode(",",$id_list);
        }else{
            $img_id="";
        }
        //添加数据
        $inserInfo=Trends::insert(['trends_id'=>null,'user_id'=>1,'trends_time'=>time(),'trends_img'=>$img_id,'trends_type'=>1,'trends_content'=>$text]);
        //判断是否成功
        if($inserInfo)
        {
           return  $this->success('添加成功');
        }else{
           return $this->fail(206,'添加动态失败');
        }
    }
    public function addQiNiu($request)
    {

        $img=$this->UploadImg($request);
        return $img;
    }
    public function addImg($request)
    {
        $img=$this->UploadImg($request);
        //循环拼接数据去添加
        foreach($img as $v)
        {
            $ImgArray[]['img_url']=$v;
        }
        //一下添加好几条数据
        $ImgInfo=DB::table("re_image")->insert($ImgArray);
        //判断添加是否成功
        if($ImgInfo!==true)
        {
            return $this->fail(206,'添加视频或照片失败');
        }
        //获取刚添加的id
        $ImgId=Image::get(['img_url','id'])
            ->whereIn("img_url",$img)
            ->toArray();
        //使用数组函数把id挑出来
        $id_list = array_column($ImgId, 'id');
        //把id换成1,2,3类型
        $img_id=implode(",",$id_list);
        return $img_id;
    }

}